<?php
/**
 * 字符串处理类
 * 
 * @version $Id: String.php 433 2014-04-01 02:01:51Z linsir123 $
 * @package Common
 */
class lpString
{
	/**
	 * [验证]邮箱格式
	 * 
	 * @param string $string 待验证的字符串
	 * @return bool
	 * @static 
	 */
	static function isEmail($string)
	{
		$pattern = "/^([a-z0-9]+[\.\-_]?)*[a-z0-9]+@([a-z0-9]+[\.\-_]?)*[a-z0-9]+\.[a-z]{2,4}$/i";
		///
		return preg_match($pattern, $string);
	}
	
	/**
	 * [验证]时间格式
	 * 
	 * @param string $string 待验证的字符串
	 * @return bool
	 * @static 
	 */
	static function isDatetime($string)
	{
		$pattern = "/^[0-9]{4}(\-|\/)[0-9]{1,2}(\\1)[0-9]{1,2}(|\s+[0-9]{1,2}(:[0-9]{1,2}){0,2})$/i";
		///
		return preg_match($pattern, $string);
	}
	
	/**
	 * [验证]日期合法性
	 * 
	 * @param string $string 待验证的字符串
	 * @param string $sep 日期的分隔符
	 * @return bool
	 * @static 
	 */
	static function isDate($string, $sep = '-')
	{
		$parts = explode($sep, $string);
		if (count($parts) == 3) {
			if (checkdate(intval($parts[1]), intval($parts[2]), intval($parts[0]))) 
				return true;
		}
		
		///
		return false;
	}
	
	/**
	 * [验证]utf-8编码
	 * 
	 * @param string $string 待验证的字符串
	 * @return bool
	 * @static 
	 */
	static function isUtf8($string)
	{
		return (utf8_encode(utf8_decode($string)) == $string);
	}
	
	/**
	 * 去除字符串的空格
	 * 
	 * @param string $string 待处理的字符串
	 * @return string
	 * @static 
	 */
	static function trim($string)
	{
		return preg_replace("/\s/", '', trim($string));
	}
	
	/**
	 * 使用反斜线引用字符串
	 * 
	 * @param mixed $var 待处理的数据
	 * @param bool $force 是否强制添加
	 * @return mixed
	 * @static 
	 */
	static function addslashes($var, $force = false)
	{
		if ( ! get_magic_quotes_gpc() || $force) 
			$var = is_array($var) ?
				array_map(array('lpString', 'addslashes'), $var) :
				addslashes($var);
			
		///
		return $var;
	}
	
	/**
	 * 反转义使用反斜线引用的字符串
	 * 
	 * @param mixed $var 待处理的数据
	 * @return mixed
	 * @static 
	 */
	static function stripslashes($var)
	{
		$var = is_array($var) ?
			array_map(array('lpString', 'stripslashes'), $var) :
			stripslashes($var);
			
		///
		return $var;
	}
	
	/**
	 * SQL引号
	 * 
	 * @param mixed $var 待处理的数据
	 * @return mixed
	 * @static 
	 */
	static function dbQuote($var)
	{
		$var = is_array($var) ?
			array_map(array('lpString', 'dbQuote'), $var) :
			'`'.trim($var).'`';
			
		///
		return $var;
	}
	
	/**
	 * 转义SQL变量
	 * 
	 * @param mixed $var 待处理的数据
	 * @return mixed
	 * @static 
	 */
	static function sqlEscape($var)
	{
		if (is_bool($var)) 
			$var = $var ? 1 : 0;
		elseif (is_null($var)) 
			$var = 'NULL';
		elseif (is_string($var))
			$var = "'".addslashes($var)."'";
		elseif (is_array($var))
			$var = array_map(array('lpString', 'sqlEscape'), $var);
			
		///
		return $var;
	}
	
	/**
	 * 截取字符串(以一个汉字为又字节，并不区分为UTF-8下的三字节)
	 * 
	 * @param string $string 待处理的字符串
	 * @param int $length 截取长度,单位为字节
	 * @param string $omission 数据省略时的替代内容
	 * @param string $code 编码
	 * @return string
	 * @static 
	 */
	static function substr($string, $length = 50, $omission = ' ...', $code = 'UTF-8')
	{
		$string = preg_replace("/\s/", ' ', strip_tags($string));
		$string = str_replace(array("&nbsp;"), array(""), $string);
		$string = htmlspecialchars_decode($string);
		if (empty($length) || strlen($string) <= $length) 
			return $string;
			
		///
		$byteLen = 0;
		$wordString = '';
		if (strtoupper($code) == 'UTF-8') {
			preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $words);
			$count = count($words[0]);
			for ($i = 0; $i < $count; $i++) {
				$wordString .= $words[0][$i];
				if (ord($words[0][$i]) >= 128) 
					$byteLen += 2;
				else
					$byteLen++;
					
				if ($byteLen >= $length) 
					return $wordString . $omission;
			}
		} else {
			$count = strlen($string);
			for ($i = 0; $i < $count; $i++) {
				if(ord(substr($string, $i, 1)) >= 128) {
					$byteLen += 2;
					$wordString .= substr($string, $i, 2);
					$i++;
				} else {
					$byteLen++;
					$wordString .= substr($string, $i, 1);
				}
				
				if ($byteLen >= $length) 
					return $wordString . $omission;
			}
		}
		
		///
		return $string;
	}
	
	/**
	 * 将16进制颜色值转成RGB格式
	 * // `Captcha.php`,`Image.php`组件有用到
	 * 
	 * @param string $color 颜色值(16进制)
	 * @param string $defualt 默认颜色值(16进制)
	 * @return array
	 * @static 
	 */
	static function hex2rgb($color, $defualt = 'ffffff')
    {
		$color = strtolower($color);
		if (substr($color, 0, 2) == '0x')
		    $color = substr($color, 2);
		elseif (substr($color, 0, 1) == '#')
		    $color = substr($color, 1);
		    
		///
		$len = strlen($color);
		if ($len == 3) 
		    return array(
		    	hexdec(substr($color, 0, 1)), 
		    	hexdec(substr($color, 1, 1)), 
		    	hexdec(substr($color, 2, 1))
		    );
		elseif ($len != 6) 
		    $color = $defualt;
		    
		///
		return array(
			hexdec(substr($color, 0, 2)), 
			hexdec(substr($color, 2, 2)), 
			hexdec(substr($color, 4, 2))
		);
    }
}
